#include "SoulDrawCacheData.h"


SoulDrawCacheData* SoulDrawCacheData::mInstance = NULL;

void SoulDrawCacheData::CreateInstance()
{
	if (mInstance == nullptr)
	{
		mInstance = new SoulDrawCacheData();
	}
}

SoulDrawCacheData* SoulDrawCacheData::GetInstance()
{
	return mInstance;
}

SoulDrawCacheData::SoulDrawCacheData()
	: m_maxCount(0)
{
}

SoulDrawCacheData::~SoulDrawCacheData()
{
	Clear();
}

bool SoulDrawCacheData::Reload()
{
	_safe_guard_begin;

	Clear();

	Initialization();

	_safe_guard_end;

	return false;
}

void SoulDrawCacheData::Clear()
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.begin();
	for (; itor != m_mapCacheData.end(); ++itor)
	{
		_safe_delete(itor->second);
	}

	m_mapCacheData.clear();

	_safe_guard_end;
}

bool SoulDrawCacheData::Initialization()
{
	_safe_guard_begin;

	Json::Value _jv = ReadJsonFile("./data/activity/soulDraw.json");
	if (_jv == Json::nullValue)
	{
		LOG_ERROR("default","ReadJsonFile -> data/activity/soulDraw.json failed ......");
		return false;
	}

	m_maxCount = _jv.size();

	Json::Value::Members _tmp = _jv.getMemberNames();
	uint32_t _size = (uint32_t)(_tmp.size());
	for (uint32_t i = 0; i < _size; ++i)
	{
		uint32_t _idx = (uint32_t)atoi(_tmp[i].c_str());

		AddData(_idx, _jv[_tmp[i]]);
	}

	return true;

	_safe_guard_end;

	return false;
}


void SoulDrawCacheData::AddData(uint32_t id, Json::Value& jm)
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.find(id);
	if (itor == m_mapCacheData.end())
	{
		SoulDraw* p = new SoulDraw;
		_safe_check(p);

		p->mId = jm["id"].asUInt();
		p->mHeroRate = jm["hero_rate"].asUInt();
		p->mHeroCard = jm["hero_card"].asUInt();

		// [106000100,106000200,106000300]
		Json::Value soulStone1 = jm["soul_stone1"];
		uint32_t sz = soulStone1.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mSoulStone1[i] = soulStone1[i].asUInt();
		}

		// [106000100,106000200,106000300]
		Json::Value soulStone2 = jm["soul_stone2"];
		sz = soulStone2.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mSoulStone2[i] = soulStone2[i].asUInt();
		}

		// [106000100,106000200,106000300]
		Json::Value soulStone3 = jm["soul_stone3"];
		sz = soulStone3.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mSoulStone3[i] = soulStone3[i].asUInt();
		}

		// [106000100,106000200,106000300]
		Json::Value soulStone4 = jm["soul_stone4"];
		sz = soulStone4.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mSoulStone4[i] = soulStone4[i].asUInt();
		}

		// [106000100,106000200,106000300]
		Json::Value soulStone5 = jm["soul_stone5"];
		sz = soulStone5.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mSoulStone5[i] = soulStone5[i].asUInt();
		}

		// [106000100,106000200,106000300]
		Json::Value soulStone6 = jm["soul_stone6"];
		sz = soulStone6.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mSoulStone6[i] = soulStone6[i].asUInt();
		}

		// [106000100,106000200,106000300]
		Json::Value soulStone7 = jm["soul_stone7"];
		sz = soulStone7.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mSoulStone7[i] = soulStone7[i].asUInt();
		}

		p->mStonePanta = jm["stone_panta"].asUInt();
		p->mStoneDecuple = jm["stone_decuple"].asUInt();

		// 
		Json::Value expRate = jm["exp_rate"];
		sz = expRate.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mExpRate[i] = expRate[i].asUInt();
		}

		// [[102000100,5,100],[102000200,5,100]]
		Json::Value expMaterial = jm["exp_material"];
		sz = expMaterial.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			Json::Value vv = expMaterial[i];
			uint32_t szz = vv.size();
			Condition3x c;
			for (uint32_t n = 0; n < szz; ++n)
			{
				c.mValue[n] = vv[n].asUInt();
			}
			p->mExpMaterial.push_back(c);
		}

		// [[102000100,5,100],[102000200,5,100]]
		Json::Value goldMaterial = jm["gold_material"];
		sz = goldMaterial.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			Json::Value vv = goldMaterial[i];
			uint32_t szz = vv.size();
			Condition3x c;
			for (uint32_t n = 0; n < szz; ++n)
			{
				c.mValue[n] = vv[n].asUInt();
			}

			p->mGoldMaterial.push_back(c);
		}

		p->mPropDouble = jm["prop_double"].asUInt();

		p->mPropTriple = jm["prop_triple"].asUInt();

		p->mPropUltra = jm["prop_ultra"].asUInt();
		// 
		p->mDiamondCost = jm["diamond_cost"].asUInt();

		//
		Json::Value giveExp = jm["give_exp"];
		sz = giveExp.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mGiveExp[i] = giveExp[i].asUInt();
		}

		m_mapCacheData[id] = p;
	}

	_safe_guard_end;
}

SoulDraw* SoulDrawCacheData::GetData(uint32_t id)
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.find(id);
	if (itor != m_mapCacheData.end())
	{
		return itor->second;
	}
	_safe_guard_end;

	return nullptr;
}

bool SoulDrawCacheData::GetDataJsonString(uint32_t id, Json::Value& _jm)
{
	_safe_guard_begin;

	SoulDraw* p = GetData(id);
	if (nullptr == p)
	{
		LOG_ERROR("default","Error15");
		return false;
	}

	_jm.clear();

	_jm["id"] = Json::Value(p->mId);


	_safe_guard_end;

	return true;
}

uint32_t SoulDrawCacheData::GetDataCount()
{
	_safe_guard_begin;

	return m_maxCount;

	_safe_guard_end;
}